package logic.common.missions;

import org.fenggui.util.Color;

import com.jme.math.Vector3f;

import fileHandling.language.LanguageLoader;
import fileHandling.language.interfaces.HUDText;
import logic.common.player.Player;
import logic.common.team.Team;
import logic.nodes.DestructionEvent;
import logic.nodes.DestructionListener;
import logic.nodes.TeamNode;

public class DestroyMission extends Mission {
	
	protected TeamNode target;
	protected DestructionListener destructionListener;
	
	public DestroyMission(Team team, int money, int assistMoney, TeamNode target) {
		super(team, money, assistMoney);
		this.target = target;
		
		addDestructionListener();
	}
	
	protected void addDestructionListener() {
		final Mission thisMission = this;
		destructionListener = new DestructionListener() {
			@Override
			public void destroyed(DestructionEvent event) {
				Player p = event.getKiller();
				if(p != null && p.getTeam() == team) {
					missionAccomplished(p);
					for(Player assist : target.getShooters()) {
						assert(p != assist);
						assist.assist(thisMission);
					}
				} else team.deleteMission(thisMission);
				target.removeDestructionListener(destructionListener);
			}
		};
		target.addDestructionListener(destructionListener);
	}
	
	public TeamNode getTarget() { return target; }
	
	@Override
	protected String getText() {
		String destroy = LanguageLoader.get(HUDText.Destroy);
		return destroy + " " + target.getName();
	}
	
	@Override
	public void missionActivated(boolean active, Player player) {
		super.missionActivated(active, player);
		if(player == team.getIngameState().getPlayer()) target.activateMissionAngles(active);
	}
	
	@Override
	public Vector3f getMissionLocation() { return target.getLocalTranslation(); }
	
	@Override
	public String[] getInfoTexts() {
		String destroyed = LanguageLoader.get(HUDText.You_Destroyed);
		return new String[] { destroyed, target.getName() };
	}
	
	@Override
	public Color[] getInfoColors() {
		return new Color[] { Color.WHITE, Color.LIGHT_RED };
	}
	
	@Override
	public String[] getAssistTexts() {
		String destroyed = LanguageLoader.get(HUDText.Destroy_Assist);
		return new String[] { destroyed, target.getName() };
	}
	
	@Override
	public Color[] getAssistColors() {
		return new Color[] { Color.WHITE, Color.LIGHT_RED };
	}
	
	@Override
	public boolean deleteAfterComplete() { return true; }
}